package br.inf.ufrgs.br.usixml4cdc.rendering;

import java.awt.Component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.w3c.dom.Element;

import br.inf.ufrgs.usixml4cdc.exceptions.ParsingErrorException;
import br.inf.ufrgs.usixml4cdc.exceptions.RenderingErrorException;


public abstract class UsiXMLElementRenderer{
	
	public abstract String getValidElementName();
	public abstract Object processRenderization(Element element) 
		throws ParsingErrorException,RenderingErrorException;
	public abstract boolean validateElement(Element element);
	public abstract UsiXMLElementRenderer getNextRenderer();
	
	protected UsiXMLInterfaceRenderer usiXMLInterfaceRenderer = null;
	
	public UsiXMLElementRenderer(UsiXMLInterfaceRenderer usiXMLInterfaceRenderer) {
		this.usiXMLInterfaceRenderer = usiXMLInterfaceRenderer;
	}
	
	public Object renderElement(Element element) 
		throws RenderingErrorException, ParsingErrorException{
	
		if (element.getTagName().equals(this.getValidElementName())){
			return this.processRenderization(element);
		}else{
			return this.getNextRenderer().renderElement(element);
		}
		
	};
	
	protected void insertContent(Element element, Component button, Method method) {
		
		ResourceRetriever resourceRetriever = this.usiXMLInterfaceRenderer.resourceRetriever;
		String resourceContent = resourceRetriever.getStringResourceContent(element);
		
		try {
			method.invoke(button, new Object[]{resourceContent});
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
	}
}
